<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">发行批次</label>
        <el-input v-model="query.imBatch" clearable placeholder="发行批次" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">发行令牌</label>
        <el-input v-model="query.imToken" clearable placeholder="发行令牌" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">发行简介</label>

        <el-input v-model="query.imBrief" clearable placeholder="发行简介" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">发行状态</label>
        <el-select v-model="query.imCode" filterable placeholder="请选择">
          <el-option
            v-for="item in dict.im_code"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select><br>
        <label class="el-form-item-label">发行通知</label>
        <el-input v-model="query.imNotice" clearable placeholder="发行通知" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">发行人</label>
        <el-input v-model="query.imUser" clearable placeholder="发行人" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="发行批次" prop="imBatch">
            <el-input-number v-model="form.imBatch" :controls="false" :min="0" :max="9999" style="width: 370px;" />          </el-form-item>
          <el-form-item label="发行令牌" prop="imToken">
            <el-input-number v-model="form.imToken" :controls="false" :min="0" :max="9999" style="width: 370px;" />          </el-form-item>
          <el-form-item label="发行简介" prop="imBrief">
            <el-input v-model="form.imBrief" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="发行状态">
            <el-radio v-for="item in dict.im_code" :key="item.id" v-model="form.imCode" :label="item.value">{{ item.label }}</el-radio>
          </el-form-item>
          <el-form-item label="发行通知" prop="imNotice">
            <el-input v-model="form.imNotice" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="发行人">
            <el-button @click="xuanze()">选择发行人</el-button>
            <el-input v-model="form.imUser" :readonly="true" />
            <el-table v-if="us" ref="table" :data="users" style="width: 100%;">
              <el-table-column label="请选择" width="80">
                <template slot-scope="scope">
                  <el-button @click="xuan(scope.row.userName)">选择</el-button>
                </template>
              </el-table-column>
              <el-table-column prop="userName" label="用户名" />
              <el-table-column prop="nickName" label="用户昵称" />
              <el-table-column prop="dept" label="部门" />
            </el-table>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="imId" label="发行编号" />
        <el-table-column prop="imBatch" label="发行批次">
          <template slot-scope="scope">
            {{ scope.row.imBatch }}
          </template>
        </el-table-column>
        <el-table-column prop="imToken" label="发行令牌">
          <template slot-scope="scope">
            {{ scope.row.imToken }}
          </template>
        </el-table-column>
        <el-table-column prop="imBrief" label="发行简介" />
        <el-table-column prop="imCode" label="发行状态">
          <template slot-scope="scope">
            {{ dict.label.im_code[scope.row.imCode] }}
          </template>
        </el-table-column>
        <el-table-column prop="imNotice" label="发行通知" />
        <el-table-column prop="imUser" label="发行人" />
        <el-table-column prop="imDate" label="发行时间" />
        <el-table-column v-if="checkPer(['admin','issuanceM:edit','issuanceM:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudIssuanceM from '@/api/issuanceM'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { list } from '@/api/issuanceM'

const defaultForm = { imId: null, imBatch: null, imToken: null, imBrief: null, imCode: null, imNotice: null, imUser: null, imDate: null }
export default {
  name: 'IssuanceM',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['im_code'],
  cruds() {
    return CRUD({ title: '发行管理', url: 'api/issuanceM', idField: 'imId', sort: 'imId,desc', crudMethod: { ...crudIssuanceM }})
  },
  data() {
    return {
      us: false,
      users: [],
      permission: {
        add: ['admin', 'issuanceM:add'],
        edit: ['admin', 'issuanceM:edit'],
        del: ['admin', 'issuanceM:del']
      },
      rules: {
        imBatch: [
          { required: true, message: '发行批次不能为空', trigger: 'blur' }
        ],
        imToken: [
          { required: true, message: '发行令牌不能为空', trigger: 'blur' }
        ],
        imBrief: [
          { required: true, message: '发行简介不能为空', trigger: 'blur' }
        ],
        imNotice: [
          { required: true, message: '发行通知不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'imBatch', display_name: '发行批次' },
        { key: 'imToken', display_name: '发行令牌' },
        { key: 'imBrief', display_name: '发行简介' },
        { key: 'imCode', display_name: '发行状态' },
        { key: 'imNotice', display_name: '发行通知' },
        { key: 'imUser', display_name: '发行人' }
      ]
    }
  },
  methods: {
    xuan(a) {
      this.form.imUser = a
      this.us = !this.us
    },
    xuanze() {
      this.us = !this.us
      list().then((data) => {
        this.users = data.content
      })
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>

</style>
